<template>
  <!-- close-on-click-modal:是否可以通过点击 modal 关闭 Dialog -->
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      label-width="80px"
    >
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="dataForm.userName" placeholder="账号"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="dataForm.password"
          type="password"
          placeholder="密码"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="comfirmPassword">
        <el-input
          v-model="dataForm.comfirmPassword"
          type="password"
          placeholder="确认密码"
        ></el-input>
      </el-form-item>
      <el-form-item label="状态" size="mini" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="1">激活</el-radio>
          <el-radio :label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      
      <el-form-item style="margin-bottom: 0;" label="角色" prop="roleIdList">
        <el-select
          v-model="dataForm.roleIdList"
          style="width: 437px"
          multiple
          placeholder="请选择"
        >
          <el-option
            v-for="role in roleList"
            :key="role.roleId"
            :label="role.roleName"
            :value="role.roleId"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    var validatePassword = (rule, value, callback) => {
      if (!this.dataForm.id && !/\S/.test(value)) {
        callback(new Error('密码不能为空'))
      } else {
        callback()
      }
    }
    var validateComfirmPassword = (rule, value, callback) => {
      if (!this.dataForm.id && !/\S/.test(value)) {
        callback(new Error('确认密码不能为空'))
      } else if (this.dataForm.password !== value) {
        callback(new Error('确认密码与密码输入不一致'))
      } else {
        callback()
      }
    }
    return {
      visible: false,
      roleList: [],
      dataForm: {
        id: 0,
        mobile: '',
        userName: '',
        password: '',
        comfirmPassword: '',
        salt: '',
        roleIdList: [],
        status: 1
      },
      dataRule: {
        userName: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        password: [{ validator: validatePassword, trigger: 'blur' }],
        comfirmPassword: [{ validator: validateComfirmPassword, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.$http({
        url: this.$http.adornUrl('/sys/role/select'),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({data})=>{
        this.roleList = data && data.code === 0 ? data.list : []
      }).then(()=>{
        this.visible = true
        this.$nextTick(()=>{
          this.$refs['dataForm'].resetFields()
        })
      }).then(()=>{
        if(this.dataForm.id){
          this.$http({
            url: this.$http.adornUrl(`/sys/user/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data})=>{
            if(data && data.code === 0){
              this.dataForm.userName = data.user.username
              this.dataForm.mobile = data.user.mobile
              this.dataForm.salt = data.user.salt
              this.dataForm.status = data.user.status
              this.dataForm.roleIdList = data.user.roleIdList
            }
          })
        }
      })
    },
    dataFormSubmit(){
      this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            //var rolestr = this.dataForm.roleIdList
            //将角色id进行数组处理
            //var roleArray = eval("(" + "[" + rolestr + "]" + ")");
            this.$http({
              url: this.$http.adornUrl(`/sys/user/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.id || undefined,
                'username': this.dataForm.userName,
                'mobile' : this.dataForm.mobile,
                'password': this.dataForm.password,
                'salt': this.dataForm.salt,
                'status': this.dataForm.status,
                //'roleIdList': roleArray ? roleArray : null
                'roleIdList': this.dataForm.roleIdList
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
    }

  }
}
</script>

<style scoped>
</style>
